package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Colision.<br>
 */
public class AccionesColision extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción ColisionBuscarI.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionBuscarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "colisionBuscar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FColisionBuscar fFColisionBuscar = (FColisionBuscar)sess.getAttribute("FColisionBuscar");
             if (fFColisionBuscar==null) {
                 fFColisionBuscar = new FColisionBuscar();
                 sess.setAttribute("FColisionBuscar", fFColisionBuscar);
             } else {
                 fFColisionBuscar.reset(mapping, request);
             }
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "colisionCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FColision fFColision = (FColision)sess.getAttribute("FColision");
             if (fFColision==null) {
                 fFColision = new FColision();
                 sess.setAttribute("FColision", fFColision);
             } else {
                 fFColision.reset(mapping, request);
             }

             //Feed for combo listOferta.
             {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
                (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
                "select new ve.usb.cohesion.runtime.Pair(periodo, idOferta) " + 
                "from Oferta").list();
              list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
              fFColision.setListOferta(list);
             }

            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionBuscar.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionBuscar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "ColisionListarI";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FColisionBuscar fFColisionBuscar = (FColisionBuscar)form;

            request.getSession().setAttribute("ColisionBuscar", " where oferta1 like '%" + fFColisionBuscar.getOferta1() + "%'");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionListarI.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionListarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "colisionListar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String crit = (String)request.getSession().getAttribute("ColisionBuscar");
            if(Integer.parseInt(request.getSession().getAttribute("tipoUsuario").toString()) != 0){
                if (crit==null) crit = " where oferta1.planDeEstudios.coordinacion = "
                                       +request.getParameter("idCoordinacion")
                                       +" order by gravedad";
                else request.getSession().removeAttribute("OfertaBuscar");
            }else{
                if (crit==null) crit = " order by gravedad";
                else request.getSession().removeAttribute("ColisionBuscar");
            }
            java.util.List<Colision> list = (java.util.List<Colision>)s.createQuery("from Colision"+crit).list();
            request.setAttribute("L_Colision", list);
            
            for (Colision colision:list) {
                if (colision.getOferta1()!=null) colision.getOferta1().getRequisito().getAsignatura().getCodigo();
            }
            for (Colision colision:list) {
                if (colision.getOferta2()!=null) colision.getOferta2().getRequisito().getAsignatura().getCodigo();
            }
            
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "colision";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idColision");
            Long idColision = (id==null) ? 
                (Long)request.getSession().getAttribute("idColision") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FColision fFColision = (FColision)sess.getAttribute("FColision");
            if (fFColision==null) {
                fFColision = new FColision();
                sess.setAttribute("FColision", fFColision);
            } else {
                fFColision.reset(mapping, request);
            }
            Colision colision = (Colision)s.load(Colision.class, idColision);
            fFColision.setIdColision(colision.getIdColision());
            if (colision.getOferta1()!=null) {
                fFColision.setOferta1(colision.getOferta1().getIdOferta());
            } else {
                fFColision.setOferta1(0);
            }
            if (colision.getOferta2()!=null) {
                fFColision.setOferta2(colision.getOferta2().getIdOferta());
            } else {
                fFColision.setOferta2(0);
            }
            fFColision.setCausa(colision.getCausa());
            fFColision.setGravedad(colision.getGravedad());

            //Feed for combo listOferta.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(periodo.nombre, idOferta) " + 
               "from Oferta").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFColision.setListOferta(list);
            }

            request.getSession().setAttribute("idColision", idColision);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "colision";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FColision fFColision = (FColision)form;

            Colision colision = (Colision)s.load(Colision.class, new Long(fFColision.getIdColision()));
            if (fFColision.getOferta1()==0) colision.setOferta1(null);
            else if ((colision.getOferta1()==null) ||
                    (colision.getOferta1().getIdOferta()!=fFColision.getOferta1())) {
                Oferta obj = (Oferta)s.load(Oferta.class, fFColision.getOferta1());
                colision.setOferta1(obj);
            }
            if (fFColision.getOferta2()==0) colision.setOferta2(null);
            else if ((colision.getOferta2()==null) ||
                    (colision.getOferta2().getIdOferta()!=fFColision.getOferta2())) {
                Oferta obj = (Oferta)s.load(Oferta.class, fFColision.getOferta2());
                colision.setOferta2(obj);
            }
            colision.setCausa(fFColision.getCausa());
            colision.setGravedad(fFColision.getGravedad());


            //Feed for combo listOferta.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(periodo, idOferta) " + 
               "from Oferta").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFColision.setListOferta(list);
            }

            request.getSession().setAttribute("idColision", colision.getIdColision());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "ColisionListarI";
        final String SALIDA_1 = "colision";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idColision");
            Long idColision = (id==null) ? 
                (Long)request.getSession().getAttribute("idColision") :
                new Long(id);
            Colision colision = (Colision)s.load(Colision.class, idColision);
            s.delete(colision);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción ColisionCrear.
     * Registro de la creación de una colision.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ColisionCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "colision";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FColision fFColision = (FColision)form;

            Colision colision = new Colision();
            if (fFColision.getOferta1()==0) colision.setOferta1(null);
            else if ((colision.getOferta1()==null) ||
                    (colision.getOferta1().getIdOferta()!=fFColision.getOferta1())) {
                Oferta obj = (Oferta)s.load(Oferta.class, fFColision.getOferta1());
                colision.setOferta1(obj);
            }
            if (fFColision.getOferta2()==0) colision.setOferta2(null);
            else if ((colision.getOferta2()==null) ||
                    (colision.getOferta2().getIdOferta()!=fFColision.getOferta2())) {
                Oferta obj = (Oferta)s.load(Oferta.class, fFColision.getOferta2());
                colision.setOferta2(obj);
            }
            colision.setCausa(fFColision.getCausa());
            colision.setGravedad(fFColision.getGravedad());

            s.save(colision);
            s.flush();
            fFColision.setIdColision(colision.getIdColision());

            //Feed for combo listOferta.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(periodo, idOferta) " + 
               "from Oferta").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFColision.setListOferta(list);
            }

            request.getSession().setAttribute("idColision", colision.getIdColision());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

}